package com.lanju.app.service;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.dxl.common.utils.MD5;
import com.lanju.app.message.bean.B001Request;
import com.lanju.app.message.bean.B001Response;
import com.lanju.entity.User;

/**
 * 用户登录
 * @author fly
 *
 */
@Service("B001Service")
public class B001Service extends BaseService<B001Request,B001Response>{
		
	@Override
	public B001Response doService(B001Request request, User u) throws Exception {
		B001Response res = new B001Response();
		
		if (StringUtils.isBlank(request.getName())) {
			res.setMessage("抱歉，请输入登陆用户名！");
			return res;
		}
		
		if ( StringUtils.isBlank(request.getPassword())) {
			res.setMessage("抱歉，请输入登陆密码！");
			return res;
		}
		
		List<User> users = dao.listByProp(User.class, "loginName", request.getName());
		if(null==users || users.size()==0){
			res.setMessage("用户不存在！");
			return res;
		}
		User user = users.get(0);
		if(!user.getPassword().equals(MD5.crypt(request.getPassword()))){
			res.setMessage("密码不正确！");
			return res;
		}
		
		user.setDeviceType(request.getDeviceType());
		user.setLastLoginDate(new Date());
		dao.update(user);
		
		res.setToken(user.getId());
		res.setLogo(user.getLog());
		res.setMobile(user.getMobile());
		res.setSex(user.getSex());
		res.setRealName(user.getName());
		res.setCarNum(user.getCarNum());
		res.setUpdateDate(user.getLastLoginDate().getTime());	
		res.setSuccess(true);
		return res;	
	}
}
